package hu.elte.inf.holiuml.gui;

import hu.elte.inf.holiuml.model.ClassDiagram;
import hu.elte.inf.holiuml.model.ObjectNode;
import hu.elte.inf.holiuml.model.UMLClass;
import hu.elte.inf.holiuml.model.UMLObject;

import javax.swing.*;

import java.awt.*;
import java.util.Vector;

import hu.elte.inf.holiuml.model.ClassNode;
/**
 * @author Istvan Bozo
 */
public class ObjectNodeOptionsDialog extends OptionsDialog{

	private JPanel propPanel;
	JComboBox nameCombo;
	JComboBox classCombo;
	JCheckBox showAttrCheckBox;
	private Vector<ClassDiagram> classDiagrams;
	private ObjectNode objectNode;
	private Vector<ClassNode> classNodes = new Vector<ClassNode>();
	private Vector<String> classNames = new Vector<String>();
	public ObjectNodeOptionsDialog(HoliUML parent, ObjectNode objectNode){
		super(parent, "Object Node Options");
		if(objectNode != null){
			this.objectNode = objectNode;
		}
		classDiagrams = super.parent.getDocument().getClassDiagram();
		setClassNodes();
		setClassNames();
		createPropPanel();
		fillPanels();
		setSize(new Dimension(450,200));
		setModalityType(ModalityType.APPLICATION_MODAL);
		if(objectNode != null){
			nameCombo.addItem(objectNode.getUMLObject().getName());
			nameCombo.setSelectedIndex(0);
		}
		setVisible(true);
	}
	private void setClassNames(){
		if(classNodes.size() != 0){
			for(int i = 0; i < classNodes.size(); i++){
				classNames.add(classNodes.get(i).getUMLClass().getName());
			}
		}
	}
	
	private void setClassNodes(){
		for(int i = 0; i < classDiagrams.size(); i++){
			Vector<ClassNode> tempClassNodes = classDiagrams.get(i)
					.getClassNodes();
			for(int j = 0; j < tempClassNodes.size(); j++){
				classNodes.add(tempClassNodes.get(j));
			}
		}
	}
	private void createPropPanel(){
		propPanel = new JPanel(new GridLayout(4,2));
		nameCombo = new JComboBox();
		nameCombo.setEditable(true);
		classCombo = new JComboBox(classNames);
		classCombo.setEditable(true);
		JButton attrButton = new JButton("Add attribute");
		showAttrCheckBox = new JCheckBox("",true);
		propPanel.add(new JLabel("Name of the object:"));
		propPanel.add(nameCombo);
		propPanel.add(new JLabel("Name of the corresponding class:"));
		propPanel.add(classCombo);
		propPanel.add(new JLabel("Attributes of the object:"));
		propPanel.add(attrButton);
		propPanel.add(new JLabel("Show attributes of the object?"));
		propPanel.add(showAttrCheckBox);
		
	}
	private void fillPanels(){
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add("North", propPanel);
		getContentPane().add("South", buttonPanel);
	}
	
	public ObjectNode createData(){
		if(objectNode == null){
			UMLObject umlObject = new UMLObject();
			umlObject.setName(nameCombo.getSelectedItem().toString());
			umlObject.setUMLClass(parent.getDocument().getModel().searchUMLClass(classCombo.getSelectedItem().toString()));
			objectNode = new ObjectNode();
			objectNode.setUMLObject(umlObject);
			objectNode.setVisibleAttributes(showAttrCheckBox.isSelected());
		}
		return objectNode;
	}
	
	public void modifyData(ObjectNode objectNode){
		objectNode.getUMLObject().setName(nameCombo.getSelectedItem().toString());
		objectNode.getUMLObject().setUMLClass(parent.getDocument().getModel().searchUMLClass(classCombo.getSelectedItem().toString()));
		objectNode.setVisibleAttributes(showAttrCheckBox.isSelected());
	}
	
	protected boolean processOK(){
		UMLClass umlClass = parent.getDocument().getModel().searchUMLClass(classCombo.getSelectedItem().toString());
		if(umlClass == null){
			JOptionPane.showMessageDialog(parent, "UMLClass with the given name does not exist in the repository!", "Error!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		return true;
	}
	protected void processCancel(){}
}
